package com.ruoyi.web.controller.admin;

import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.TokenService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PetAdoption;
import com.ruoyi.system.service.IPetAdoptionService;

import javax.servlet.http.HttpServletRequest;

/**
 * 领养申请Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/adopt")
public class AdoptController extends BaseController
{
    @Autowired
    private IPetAdoptionService petAdoptionService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询领养申请列表
     */
    @PreAuthorize("@ss.hasPermi('pet:adopt:list')")
    @GetMapping("/list")
    public TableDataInfo list(PetAdoption petAdoption)
    {
        startPage();
        List<PetAdoption> list = petAdoptionService.selectPetAdoptionList(petAdoption);
        return getDataTable(list);
    }

    /**
     * 查询领养申请列表
     */
    @GetMapping("/listMyAdoption")
    public TableDataInfo listMyAdoption(PetAdoption petAdoption)
    {
        startPage();
        petAdoption.setUserId(SecurityUtils.getUserId());
        List<PetAdoption> list = petAdoptionService.selectPetAdoptionList(petAdoption);
        return getDataTable(list);
    }

    /**
     * 获取领养申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('pet:adopt:query')")
    @GetMapping(value = "/get/{adoptionId}")
    public AjaxResult getInfo(@PathVariable("adoptionId") Long adoptionId)
    {
        return success(petAdoptionService.selectPetAdoptionById(adoptionId));
    }

    /**
     * 新增领养申请
     */
    @PreAuthorize("@ss.hasPermi('pet:adopt:add')")
    @Log(title = "领养申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody PetAdoption petAdoption)
    {
        PetAdoption  petAdoptionOld= petAdoptionService.getByPetId(petAdoption.getPetId());
        if(ObjectUtils.isNotEmpty(petAdoptionOld)){
            throw new BaseException("当前动物已经被领养");
        }
        return toAjax(petAdoptionService.insertPetAdoption(petAdoption));
    }

    /**
     * 修改领养申请
     */
    @PreAuthorize("@ss.hasPermi('pet:adopt:edit')")
    @Log(title = "领养申请", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody PetAdoption petAdoption)
    {
        return toAjax(petAdoptionService.updatePetAdoption(petAdoption));
    }

    /**
     * 删除领养申请
     */
    @PreAuthorize("@ss.hasPermi('pet:adopt:remove')")
    @Log(title = "领养申请", businessType = BusinessType.DELETE)
    @PostMapping("/del/{adoptionIds}")
    public AjaxResult remove(@PathVariable Long[] adoptionIds)
    {
        return toAjax(petAdoptionService.deletePetAdoptionByIds(adoptionIds));
    }

    /**
     * 审批领养申请
     */
    @PreAuthorize("@ss.hasPermi('pet:adopt:approve')")
    @Log(title = "领养申请", businessType = BusinessType.UPDATE)
    @PostMapping("/approve")
    public AjaxResult approve(@RequestBody PetAdoption petAdoption)
    {
        return toAjax(petAdoptionService.approveAdoption(petAdoption));
    }
}
